//
//  ZTPingpaiTableViewCell.m
//  CarTown
//
//  Created by xionghao on 15/8/26.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "ZTPingpaiTableViewCell.h"
#import "ZTRadioBtn.h"
#import "PingpaiMode.h"
#import <UIButton+WebCache.h>
@interface ZTPingpaiTableViewCell ()
@property (weak, nonatomic) IBOutlet UIView *pingpaibgview;
@property (nonatomic,strong) CAShapeLayer *btnlayer;

@end


@implementation ZTPingpaiTableViewCell

+(instancetype)pingpaiWithTablView:(UITableView *)tablView{
    ZTPingpaiTableViewCell *cell=[tablView dequeueReusableCellWithIdentifier:@"pingpai"];
    if (cell==nil) {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"ZTPingpaiTableViewCell" owner:nil options:nil]lastObject];
    }
    return cell;
}

-(void)setPingpaiarray:(NSArray *)pingpaiarray{
    _pingpaiarray=pingpaiarray;
    if (_pingpaiarray.count==0) {
         self.openbtn.hidden=YES;
        return;
       
    }else{
        self.openbtn.hidden=NO;
        [self layoutview];
    }
    
}

#pragma mark - animation method
- (void)animateIndicator:(CAShapeLayer *)indicator Forward:(BOOL)forward {
    [CATransaction begin];
    [CATransaction setAnimationDuration:0.25];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithControlPoints:0.4 :0.0 :0.2 :1.0]];
    
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    anim.values = forward ? @[ @0, @(M_PI) ] : @[ @(M_PI), @0 ];
    
    if (!anim.removedOnCompletion) {
        [indicator addAnimation:anim forKey:anim.keyPath];
    } else {
        [indicator addAnimation:anim forKey:anim.keyPath];
        [indicator setValue:anim.values.lastObject forKeyPath:anim.keyPath];
    }
    
    [CATransaction commit];
    
    
}

- (CAShapeLayer *)createIndicatorWithColor:(UIColor *)color andPosition:(CGPoint)point {
    CAShapeLayer *layer = [CAShapeLayer new];
    
    UIBezierPath *path = [UIBezierPath new];
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(8, 0)];
    [path addLineToPoint:CGPointMake(4, 5)];
    [path closePath];
    
    layer.path = path.CGPath;
    layer.lineWidth = 1.0;
    layer.fillColor = color.CGColor;
    
    CGPathRef bound = CGPathCreateCopyByStrokingPath(layer.path, nil, layer.lineWidth, kCGLineCapButt, kCGLineJoinMiter, layer.miterLimit);
    layer.bounds = CGPathGetBoundingBox(bound);
    
    layer.position = point;
    
    return layer;
}

-(void)layoutview{
    
    for (UIView *view in self.pingpaibgview.subviews) {
        [view removeFromSuperview];
    }
    CGFloat defalutheight=73;
   int rows=ceilf(_pingpaiarray.count/3.0);
    CGFloat height;
    if (rows==0) {
        height=defalutheight;
    }else{
        height=rows*defalutheight;
    }
   
    CGFloat btnwidth=(XHScreenwidth-2)/3;
    CGFloat comwidth=1;
    CGFloat comheight=height;
    CGFloat comx;
    CGFloat comy=0;
    for (int i=0; i<2; i++) {
        UIView *boder=[[UIView alloc]init];
        boder.backgroundColor=BoderColor;
        comx=btnwidth*i+btnwidth;
        boder.frame=CGRectMake(comx, comy, comwidth, comheight);
        [self.pingpaibgview addSubview:boder];
    }
    CGFloat vowidth=XHScreenwidth;
    CGFloat voheight=1;
    CGFloat vox=0;
    CGFloat voy;
    for (int i=0; i<rows+1; i++) {
        UIView *boder=[[UIView alloc]init];
        boder.backgroundColor=BoderColor;
        voy=defalutheight*i+defalutheight;
        boder.frame=CGRectMake(vox, voy, vowidth, voheight);
        [self.pingpaibgview addSubview:boder];
    }
    CGFloat btnx;
    CGFloat btny;
    CGFloat btnheight=defalutheight;
    for (int i=0; i<_pingpaiarray.count; i++) {
        ZTRadioBtn *btn=[[ZTRadioBtn alloc]init];
        int row=i/3;
        int colum=i%3;
        btnx=colum*btnwidth;
        btny=defalutheight*row;
         btn.frame=CGRectMake(btnx, btny, btnwidth, btnheight);
        PingpaiMode *mode=_pingpaiarray[i];
        [btn setTitle:mode.brand_name forState:UIControlStateNormal];
        [btn sd_setImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:mode.brand_logo]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"placehoder"]];
        [btn setTitleColor:textnormalGary forState:UIControlStateNormal];
        btn.titleLabel.font=[UIFont systemFontOfSize:12];
        btn.tag=i;
        [btn addTarget:self action:@selector(pinglcilck:) forControlEvents: UIControlEventTouchUpInside];
        [self.pingpaibgview addSubview:btn];
    }
    
}


-(void)pinglcilck:(UIButton *)btn{
    if ([_delegate respondsToSelector:@selector(pingpaiItemClick:)]
        ) {
        [_delegate pingpaiItemClick:btn.tag];
    }
}

- (void)awakeFromNib {
    
    CAShapeLayer *layer=[self createIndicatorWithColor:placehodlerColor andPosition:CGPointMake(42, 13.5)];
    [self.openbtn.layer addSublayer:layer];
    [self.openbtn setTitleColor:placehodlerColor forState:UIControlStateNormal];
    self.btnlayer=layer;    // Initialization code
}
- (IBAction)openclick:(UIButton *)sender {
    sender.selected=!sender.selected;
   [self animateIndicator:self.btnlayer Forward:sender.selected];
    if ([_delegate respondsToSelector:@selector(showorhideView:)]) {
        [_delegate showorhideView:sender.selected];
    }
}


- (void)rotate:(OpenBtn *)sender {
    if (!sender.selected) {
        [UIView animateWithDuration:0.5 animations:^{
            sender.imageView.transform = CGAffineTransformMakeRotation(M_PI);
        } completion:^(BOOL finished) {
           
        }];
    }
    else {
        [UIView animateWithDuration:0.5 animations:^{
            sender.imageView.transform = CGAffineTransformMakeRotation(0);
        } completion:^(BOOL finished) {
            
        }];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
